package com.tang.service.impl;

import com.alibaba.fastjson.JSON;
import com.tang.service.NetMall;
import com.tang.utils.HttpClient;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JDNetMall extends NetMall {

    public JDNetMall(String uId, String uPwd) {
        super(uId, uPwd);
    }

    @Override
    protected Boolean login(String uId, String uPwd) {
        log.info("模拟京东用户登录！uId:{}, pWd:{}", uId, uPwd);
        return true;
    }

    @Override
    protected Map<String, String> reptile(String skuUrl) {
        String str = HttpClient.doGet(skuUrl);
        Pattern compile = Pattern.compile("(?<=title\\>).*(?=</title)");
        Matcher matcher = compile.matcher(str);
        Map<String, String> map = new ConcurrentHashMap<>();
        if (matcher.find()) {
            map.put("name", matcher.group());
        }
        map.put("price", "5900.00");
        log.info("模拟京东商品爬虫解析：{}|{} 元{}", map.get("name"), map.get("price"), skuUrl);
        return map;
    }

    @Override
    protected String createBase64(Map<String, String> goodsInfo) {
        BASE64Encoder encoder = new BASE64Encoder();
        log.info("模拟生成京东商品base64海报");
        return encoder.encode(JSON.toJSONString(goodsInfo).getBytes(StandardCharsets.UTF_8));
    }

}
